package com.ruibang.glass.produce.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.produce.constant.Constants;
import com.ruibang.glass.produce.domain.req.CommonTrendChartReq;
import com.ruibang.glass.produce.domain.req.GlassCompositionReq;
import com.ruibang.glass.produce.domain.resp.GlassCompositionTrendChart;
import com.ruibang.glass.produce.entity.ProduceGlassComposition;
import com.ruibang.glass.produce.mapper.ProduceGlassCompositionMapper;
import com.ruibang.glass.produce.service.ProduceGlassCompositionService;
import com.ruibang.glass.produce.util.ExcelStyleUtils;
//import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 生产-理化-玻璃成分表 服务实现类
 * </p>
 *
 * @author 宋锦康
 * @since 2024-03-11
 */
@Slf4j
@Service
public class ProduceGlassCompositionServiceImpl extends ServiceImpl<ProduceGlassCompositionMapper, ProduceGlassComposition> implements ProduceGlassCompositionService {
    @Autowired
    private RedisSingleService redisService;
//    @PostConstruct
//    public void initData() {
//        List<ProduceGlassComposition> list = this.list();
//        for (ProduceGlassComposition moistureDeteciton : list) {
//            String checkById = moistureDeteciton.getCheckById();
//            if (StringUtils.isNotBlank(checkById)) {
//                Object checkObj = redisService.hget(RedisKey.USER_GROUP_TYPE, checkById);
//                if (checkObj != null) {
//                    moistureDeteciton.setCheckBy(checkObj.toString());
//                }
//            }
//        }
//        updateBatchById(list);
//    }

    @Override
    public IPage<ProduceGlassComposition> pageQuery(GlassCompositionReq glassCompositionReq) {
        QueryWrapper<ProduceGlassComposition> qw = new QueryWrapper<>();
        if (glassCompositionReq.getSampleEndTime() != null && glassCompositionReq.getSampleStartTime() != null) {
            qw.lambda().between(ProduceGlassComposition::getSampleTime, glassCompositionReq.getSampleStartTime(), glassCompositionReq.getSampleEndTime());
        }
        qw.lambda().eq(ProduceGlassComposition::getIsDelete, Constants.ZERO_STR);
        qw.lambda().like(StringUtils.isNotBlank(glassCompositionReq.getItem()), ProduceGlassComposition::getItem, glassCompositionReq.getItem());
        qw.lambda().like(StringUtils.isNotBlank(glassCompositionReq.getCreateUserName()), ProduceGlassComposition::getCreateUserName, glassCompositionReq.getCreateUserName());
        qw.lambda().orderByDesc(ProduceGlassComposition::getCreateTime);
        return this.page(new Page<>(glassCompositionReq.getPageNo(), glassCompositionReq.getPageSize()), qw.lambda());
    }

    @Override
    public void delete(String id) {
        lambdaUpdate().eq(ProduceGlassComposition::getId, id).set(ProduceGlassComposition::getIsDelete, Constants.ONE_STR).update();
    }

    @Override
    public GlassCompositionTrendChart trendChart(CommonTrendChartReq trendChartReq) {
        //默认查询3天
        if (trendChartReq.getStartTime() == null || trendChartReq.getEndTime() == null) {
            LocalDateTime now = LocalDateTime.now();
            trendChartReq.setStartTime(now.minusDays(3));
            trendChartReq.setEndTime(now);
        }
        //组合数据
        GlassCompositionTrendChart glassCompositionTrendChart = new GlassCompositionTrendChart();
        List<ProduceGlassComposition> data = baseMapper.trendChart(trendChartReq);
        if (CollectionUtils.isNotEmpty(data)) {
            glassCompositionTrendChart.setData(data);
            glassCompositionTrendChart.setCollectTime(data.stream().map(e -> e.getSampleTime().format(Constants.DATE_TIME_FORMATTER)).collect(Collectors.toList()));
            //玻璃成分1
            glassCompositionTrendChart.setS1(data.stream().map(ProduceGlassComposition::getS1).collect(Collectors.toList()));
            glassCompositionTrendChart.setS2(data.stream().map(ProduceGlassComposition::getS2).collect(Collectors.toList()));
            glassCompositionTrendChart.setS4(data.stream().map(ProduceGlassComposition::getS4).collect(Collectors.toList()));
            glassCompositionTrendChart.setS5(data.stream().map(ProduceGlassComposition::getS5).collect(Collectors.toList()));
            //玻璃成分2
            glassCompositionTrendChart.setS7(data.stream().map(ProduceGlassComposition::getS7).collect(Collectors.toList()));
            glassCompositionTrendChart.setS6(data.stream().map(ProduceGlassComposition::getS6).collect(Collectors.toList()));
            glassCompositionTrendChart.setS3(data.stream().map(ProduceGlassComposition::getS3).collect(Collectors.toList()));
            glassCompositionTrendChart.setS8(data.stream().map(ProduceGlassComposition::getS8).collect(Collectors.toList()));
            glassCompositionTrendChart.setS9(data.stream().map(ProduceGlassComposition::getS9).collect(Collectors.toList()));
            glassCompositionTrendChart.setS10(data.stream().map(ProduceGlassComposition::getS10).collect(Collectors.toList()));
            glassCompositionTrendChart.setS11(data.stream().map(ProduceGlassComposition::getS11).collect(Collectors.toList()));
        }
        return glassCompositionTrendChart;
    }

    @Override
    public void export(GlassCompositionReq glassCompositionReq, HttpServletResponse response) {
        glassCompositionReq.setPageNo(-1);
        glassCompositionReq.setPageSize(-1);
        IPage<ProduceGlassComposition> page = this.pageQuery(glassCompositionReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<ProduceGlassComposition> produceGlassCompositionList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), ProduceGlassComposition.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(produceGlassCompositionList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void saveInfo(ProduceGlassComposition produceGlassComposition) {
        produceGlassComposition.setIsDelete(Constants.ZERO_STR);
        setUserName(produceGlassComposition);
        this.save(produceGlassComposition);
    }

    @Override
    public void edit(ProduceGlassComposition produceGlassComposition) {
        setUserName(produceGlassComposition);
        this.updateById(produceGlassComposition);
    }

    private void setUserName(ProduceGlassComposition produceGlassComposition) {
        String createUserId = produceGlassComposition.getCreateUserId();
        String checkById = produceGlassComposition.getCheckById();
        if (StringUtils.isNotBlank(createUserId)) {
            Object userObj = redisService.hget(RedisKey.USER_GROUP_TYPE, createUserId);
            if (userObj != null && StringUtils.isNotBlank(userObj.toString())) {
                produceGlassComposition.setCreateUserName(userObj.toString());
            }
        } else {
            produceGlassComposition.setCreateUserName("");
        }
        if (StringUtils.isNotBlank(checkById)) {
            Object userObj = redisService.hget(RedisKey.USER_GROUP_TYPE, checkById);
            if (userObj != null && StringUtils.isNotBlank(userObj.toString())) {
                produceGlassComposition.setCheckBy(userObj.toString());
            }
        } else {
            produceGlassComposition.setCheckBy("");
        }
    }
}
